﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyVoteMVC.BackstageManagement.Controllers;
using MyVoteMVC.Domain;

namespace MyVoteMVC.BackstageManagement.Views
{
    public partial class ActivityManagement : Form, IActivityManagementView
    {
        private IActivityManagementController controller;

        public ActivityManagement()
        {
            InitializeComponent();
            controller = new ActivityManagementController(this);
        }

        #region activity

        private void btnAddActivity_Click(object sender, EventArgs e)
        {
            AddActivity form = new AddActivity();
            form.OnAddComplete += (o1, o2) => 
            {
                controller.GetActivities();
            };
            form.ShowDialog();
        }

        private void btnSecondActivity_Click(object sender, EventArgs e)
        {
            if (gvActivity.SelectedRows.Count > 0 && gvActivity.SelectedRows[0].DataBoundItem != null)
            {
                VT_Activities activity = gvActivity.SelectedRows[0].DataBoundItem as VT_Activities;
                AddActivity form = new AddActivity(activity.Activity_ID);
                form.Text = "创建二次活动";
                form.OnAddComplete += (o1, o2) =>
                {
                    controller.GetActivities();
                };
                form.ShowDialog();
            }
            else
            {
                MessageBox.Show("请选择一个活动作为二次活动的父活动");
                return;
            }
           
        }

        private void btnDeleteActivity_Click(object sender, EventArgs e)
        {
            if (gvActivity.SelectedRows.Count > 0 && gvActivity.SelectedRows[0].DataBoundItem != null)
            {
                VT_Activities activity = gvActivity.SelectedRows[0].DataBoundItem as VT_Activities;
                controller.DeleteActivity(activity.Activity_ID);
            }
            else
            {
                MessageBox.Show("请选择要删除的活动");
                return;
            }
        }

        private void btnUpdateActivity_Click(object sender, EventArgs e)
        {
            if (gvActivity.SelectedRows.Count > 0 && gvActivity.SelectedRows[0].DataBoundItem != null)
            {
                VT_Activities activity = gvActivity.SelectedRows[0].DataBoundItem as VT_Activities;
                UpdateActivity form = new UpdateActivity(activity.Activity_ID);
                form.OnUpdateComplete += (o1, o2) =>
                {
                    controller.GetActivities();
                };
                form.ShowDialog();
            }
            else
            {
                MessageBox.Show("请选择要更新的活动");
                return;
            }
        }

        #endregion

        #region question

        private void btnAddQuestion_Click(object sender, EventArgs e)
        {
            if (gvActivity.SelectedRows.Count > 0 && gvActivity.SelectedRows[0].DataBoundItem != null)
            {
                VT_Activities activity = gvActivity.SelectedRows[0].DataBoundItem as VT_Activities;
                AddQuestion form = new AddQuestion(activity.Activity_ID);
                form.OnAddComplete += (o1, o2) =>
                {
                    controller.GetQuestions(activity.Activity_ID);
                };
                form.ShowDialog();
            }
            else
            {
                MessageBox.Show("请选择增加问题的活动");
                return;
            }
        }

        private void btnDeleteQuestion_Click(object sender, EventArgs e)
        {
            if (gvActivity.SelectedRows.Count > 0 && gvActivity.SelectedRows[0].DataBoundItem != null)
            {
                VT_Activities activity = gvActivity.SelectedRows[0].DataBoundItem as VT_Activities;
                if (gvQuestion.SelectedRows.Count > 0 && gvQuestion.SelectedRows[0].DataBoundItem != null)
                {
                    VT_Questions question = gvQuestion.SelectedRows[0].DataBoundItem as VT_Questions;
                    controller.DeleteQuestion(activity.Activity_ID, question.Question_ID);
                }
                else
                {
                    MessageBox.Show("请选择要删除的问题");
                    return;
                }
            }
            else
            {
                MessageBox.Show("请选择要删除问题的活动");
                return;
            }
        }

        private void btnUpdateQuestion_Click(object sender, EventArgs e)
        {
            if (gvActivity.SelectedRows.Count > 0 && gvActivity.SelectedRows[0].DataBoundItem != null)
            {
                VT_Activities activity = gvActivity.SelectedRows[0].DataBoundItem as VT_Activities;
                if (gvQuestion.SelectedRows.Count > 0 && gvQuestion.SelectedRows[0].DataBoundItem != null)
                {
                    VT_Questions question = gvQuestion.SelectedRows[0].DataBoundItem as VT_Questions;
                    UpdateQuestion form = new UpdateQuestion(activity.Activity_ID, question.Question_ID);
                    form.OnUpdateComplete += (o1, o2) =>
                    {
                        controller.GetQuestions(activity.Activity_ID);
                    };
                    form.ShowDialog();
                }
                else
                {
                    MessageBox.Show("请选择要更新的问题");
                    return;
                }
            }
            else
            {
                MessageBox.Show("请选择要更新问题的活动");
                return;
            }
        }

        #endregion

        #region item

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            if (gvQuestion.SelectedRows.Count > 0 && gvQuestion.SelectedRows[0].DataBoundItem != null)
            {
                VT_Questions question = gvQuestion.SelectedRows[0].DataBoundItem as VT_Questions;
                AddItem form = new AddItem(question.Question_ID);
                form.OnAddComplete += (o1, o2) =>
                {
                        controller.GetItems(question.Question_ID);
                };
                form.ShowDialog();
            }
            else
            {
                MessageBox.Show("请选择要增加选项的问题");
                return;
            }
        }

        private void btnDeleteItem_Click(object sender, EventArgs e)
        {
            if (gvQuestion.SelectedRows.Count > 0 && gvQuestion.SelectedRows[0].DataBoundItem != null)
            {
                VT_Questions question = gvQuestion.SelectedRows[0].DataBoundItem as VT_Questions;
                if (gvItem.SelectedRows.Count > 0 && gvItem.SelectedRows[0].DataBoundItem != null)
                {
                    VT_Items item = gvItem.SelectedRows[0].DataBoundItem as VT_Items;
                    controller.DeleteItem(question.Question_ID, item.Item_ID);

                }
                else
                {
                    MessageBox.Show("请选择要删除的选项");
                    return;
                }
            }
            else
            {
                MessageBox.Show("请选择要删除选项的问题");
                return;
            }
        }

        private void btnUpdateItem_Click(object sender, EventArgs e)
        {
            if (gvQuestion.SelectedRows.Count > 0 && gvQuestion.SelectedRows[0].DataBoundItem != null)
            {
                VT_Questions question = gvQuestion.SelectedRows[0].DataBoundItem as VT_Questions;
                if (gvItem.SelectedRows.Count > 0 && gvItem.SelectedRows[0].DataBoundItem != null)
                {
                    VT_Items item = gvItem.SelectedRows[0].DataBoundItem as VT_Items;
                    UpdateItem form = new UpdateItem(question.Question_ID, item.Item_ID);
                    form.OnUpdateComplete += (o1, o2) =>
                    {
                        controller.GetItems(question.Question_ID);
                    };
                    form.ShowDialog();
                }
                else
                {
                    MessageBox.Show("请选择要更新的选项");
                    return;
                }
            }
            else
            {
                MessageBox.Show("请选择要更新选项的问题");
                return;
            }
        }

        #endregion

        private void ActivityManagement_Load(object sender, EventArgs e)
        {
            controller.GetActivities();
        }

        private void gvActivity_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            
        }

        private void gvQuestion_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {

        }

        private void gvItem_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {

        }

        private void gvActivity_SelectionChanged(object sender, EventArgs e)
        {
            if (gvActivity.SelectedRows.Count > 0 && gvActivity.SelectedRows[0].DataBoundItem != null)
            {
                VT_Activities activity = gvActivity.SelectedRows[0].DataBoundItem as VT_Activities;
                controller.GetQuestions(activity.Activity_ID);
            }
            else
            {
                gvQuestion.DataSource = null;
            }
        }

        private void gvQuestion_SelectionChanged(object sender, EventArgs e)
        {
            if (gvQuestion.SelectedRows.Count > 0 && gvQuestion.SelectedRows[0].DataBoundItem != null)
            {
                VT_Questions question = gvQuestion.SelectedRows[0].DataBoundItem as VT_Questions;
                controller.GetItems(question.Question_ID);
            }
            else
            {
                gvItem.DataSource = null;
            }
        }

        private void gvItem_SelectionChanged(object sender, EventArgs e)
        {

        }

        #region view

        public void GetActivitysComplete(IList<VT_Activities> activities)
        {
            gvActivity.DataSource = activities;

        }

        public void DeleteActivityComplete(string message)
        {
            MessageBox.Show(message);
            controller.GetActivities();
        }

        public void DeleteQuestionComplete(string message, string activityID)
        {
            MessageBox.Show(message);
            controller.GetQuestions(activityID);
        }

        public void GetQuestionsComplete(IList<VT_Questions> questions)
        {
            gvQuestion.DataSource = questions;
        }

        public void DeleteItemComplete(string message, string questionID)
        {
            MessageBox.Show(message);
            controller.GetItems(questionID);
        }

        public void GetItemsComplete(IList<VT_Items> items)
        {
            gvItem.DataSource = items;
        }

        #endregion

        private void btnAddItemFromPorject_Click(object sender, EventArgs e)
        {
            //不需要父活动
            if (gvQuestion.SelectedRows.Count > 0 && gvQuestion.SelectedRows[0].DataBoundItem != null)
            {
                VT_Questions question = gvQuestion.SelectedRows[0].DataBoundItem as VT_Questions;
                ImportProjectToItem form = new ImportProjectToItem(question.Question_ID);
                form.OnAddComplete += (o1, o2) =>
                {
                    controller.GetActivities();
                };
                form.ShowDialog();
            }
            else
            {
                MessageBox.Show("请选择一个问题进行选项导入");
                return;
            }
        }

        private void btnAddItemFromActivity_Click(object sender, EventArgs e)
        {
            //需要父活动
            if (gvActivity.SelectedRows.Count > 0 && gvActivity.SelectedRows[0].DataBoundItem != null)
            {
                VT_Activities activity = gvActivity.SelectedRows[0].DataBoundItem as VT_Activities;
                if (!string.IsNullOrWhiteSpace(activity.Activity_PID))
                {
                    if (gvQuestion.SelectedRows.Count > 0 && gvQuestion.SelectedRows[0].DataBoundItem != null)
                    {
                        VT_Questions question = gvQuestion.SelectedRows[0].DataBoundItem as VT_Questions;
                        ImportActivityToItem form = new ImportActivityToItem(activity.Activity_ID, activity.Activity_PID, question.Question_ID);
                        form.OnAddComplete += (o1, o2) =>
                        {
                            controller.GetActivities();
                        };
                        form.ShowDialog();
                    }
                    else
                    {
                        MessageBox.Show("请选择一个问题进行选项导入");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("请选择一个存在父活动的活动进行选项导入");
                    return;
                }

            }
            else
            {
                MessageBox.Show("请选择一个活动进行选项导入");
                return;
            }
        }

        private void btnExportxls_Click(object sender, EventArgs e)
        {
            if (gvActivity.SelectedRows.Count > 0 && gvActivity.SelectedRows[0].DataBoundItem != null)
            {
                VT_Activities activity = gvActivity.SelectedRows[0].DataBoundItem as VT_Activities;
               
                controller.ExportToXls(activity.Activity_ID);
            }
            else
            {
                MessageBox.Show("请选择一个活动进行选项导出");
                return;
            }
        }


    }
}
